package com.czl.module_storehouse.activity.intostore.record;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.czl.module_base.activity.BaseActivity;
import com.czl.module_storehouse.databinding.FooterCommonRemarkBinding;
import com.czl.module_storehouse.databinding.HeaderRecordInfoBinding;
import com.czl.module_storehouse.databinding.LayoutRecyclerViewBinding;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.RecordInfoAdapter;
import com.czl.module_storehouse.bean.StorageRecordBean;
import com.czl.module_storehouse.event.WarehouseRecordEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * @author xubin
 */
public class RecordInfoActivity extends BaseActivity<LayoutRecyclerViewBinding> {

    private RecordInfoAdapter mAdapter;
    private HeaderRecordInfoBinding mHeaderBinding;
    private FooterCommonRemarkBinding mFooterBinding;

    @Override
    protected LayoutRecyclerViewBinding getBinding(LayoutInflater layoutInflater) {
        return LayoutRecyclerViewBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("入库单");
        mAdapter = new RecordInfoAdapter(R.layout.item_record_info, new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);
        mHeaderBinding = HeaderRecordInfoBinding.inflate(getLayoutInflater());
        mFooterBinding = FooterCommonRemarkBinding.inflate(getLayoutInflater());
        mFooterBinding.etRemark.setEnabled(false);
        mFooterBinding.etRemark.setHint("");
        mAdapter.addHeaderView(mHeaderBinding.getRoot());
        mAdapter.addFooterView(mFooterBinding.getRoot());
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(WarehouseRecordEvent event) {
        StorageRecordBean storageRecordBean = event.getStorageRecordBean();
        if (storageRecordBean == null) {
            return;
        }
        initHeaderView(storageRecordBean);
        mFooterBinding.etRemark.setText(storageRecordBean.getStorageConment());
        mAdapter.addData(storageRecordBean.getStorageSortViewList());
    }

    @SuppressLint("SetTextI18n")
    private void initHeaderView(StorageRecordBean bean) {
        mHeaderBinding.tvCode.setText(bean.getStorageCode());
        mHeaderBinding.tvDate.setText(bean.getStorageDateStr());
        mHeaderBinding.tvStr.setText("入库类型：" + bean.getStorageModelStr());
        mHeaderBinding.tvSupplier.setText("供应商：" + bean.getSupplier());
        mHeaderBinding.tvTransmitter.setText("送货人：" + bean.getTransmitter());
        if (StorageRecordBean.STORAGE_MODEL_OTHER == bean.getStorageModel()) {
            mHeaderBinding.tvStoreCode.setVisibility(View.GONE);
        } else if (StorageRecordBean.STORAGE_MODEL_PURCHASE == bean.getStorageModel()) {
            mHeaderBinding.tvStoreCode.setText("入库单：" + bean.getContractName());
        } else {
            mHeaderBinding.tvStoreCode.setText("自建工程：" + bean.getContractName());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHeaderBinding != null) {
            mHeaderBinding.unbind();
        }
        if (mFooterBinding != null) {
            mFooterBinding.unbind();
        }
    }
}
